
 SELECT 
   case when SO.SOG_PARIVA is null then SO.SOG_CODFIS else SO.SOG_PARIVA end "CDF/PARIVA",
   --vsc.SOG_ID SOGGETTO_UE,
   vsec.SOG_RAGSOC "RAGIONE_SOCIALE",
   '' nome,
   '' E_mail,
   'UMBRIA ENERGY' "CODICE FONTE",
   
   CASE WHEN vsec.SCR_RIFERIMENTO LIKE '%GN%' OR de.pardst_riferimento LIKE '%GN%' THEN 'GAS' ELSE
        CASE WHEN vsec.SCR_RIFERIMENTO  LIKE '%UEV%' OR de.pardst_riferimento LIKE '%UEV%' THEN 'ELETTRICITA' END END "CODICE CATEGORIA",
   
   CONCAT (FAT.FAT_ANNO,FAT.FAT_NUM) "IDENTIFICATIVO FATTURA",
   trunc(FAT.FAT_DT) "DATA EMISSIONE FATTURA", 
   FAP.PFAT_TOT "IMPORTO FATTURA",
   
      vsec.PAR_ID "N.RO PARITA",   

         trunc (vsec.MOV_DT) "DATA MOVIMENTO",   
         trunc(vsec.data_scadenza_rata) "DATA SCADENZA RATA", 
         trunc(vsec.data_pagamento) "DATA INCASSO RATA",
        
         case when vsec.rata_num is null or vsec.rata_num = 0 then vsec.SCR_IMP_D 
                   else vsec.rata_imp end "IMPORTO DARE",   

         vsec.SCR_IMP_A "IMPORTO AVERE",  
         
         --vsec.rata_imp,
         
         CASE WHEN vsec.rata_num = 0 THEN '' ELSE TO_CHAR(vsec.rata_num) END RATA_NUM,

         vsec.SCR_RIFERIMENTO "DOCUMENTO",
        case when de.pardst_riferimento <> vsec.SCR_RIFERIMENTO  then de.pardst_riferimento end "DOC.DI RIFERIMENTO",
        
  --       de.dst_des,
        

      --   "vsec"."MOV_NOTA",   

                       -- '1' "VIS_RIF",

                       -- '0' "VIS_NOTE",

                        --"vsec"."SALDO_PAR",

         --"vsec"."MOV_NUM",   

        -- "vsec"."SCR_NUM", 

        -- "vsec"."MOV_FLSTO",   

       --  "vsec"."TMOV_ID",   

        -- "vsec"."TMOV_DES",  

      --   "vsec"."TMOV_FLSTO", 

       --  "vsec"."TSCR_ID",   

         vsec.TSCR_DES  "DESCR. MOVIMENTO",
         CASE WHEN vsec.ISS_ID_PARTITA =602 THEN 'APERTA' ELSE 'CHIUSA' END STATO_PARTITA

         --"vsec"."DST_ID",   

       --  "vsec"."SCR_IMP",   

        -- "vsec"."SCR_SGN",   

    --     "vsec"."SCRDST_NUM", 

      --   "vsec"."TFAT_ID",

     --    "vsec"."INC_ID",

     --    "vsec"."RAGF_ID",

    --     "vsec"."CON_ID",

    --     "vsec"."CON_ANNO",

    --     "vsec"."CSI_ID",   

     --    "vsec"."ISTA_VERSTATO",   

 --        "vsec"."ITER_ID",   

   --      "vsec"."SCRITTURADEST_ROWID",   

   --      "vsec"."SCRITTURADEST_ROWSCN",   

 --        "vsec"."AZ_ID",   

                      

  --       "vsec"."STA_IDSYS",   

 --        "vsec"."STA_IDSYS_PARTITA",   

  --       "vsec"."ISS_ID_PARTITA"  

         --"vsec"."ODL_ID"

         --"vsec"."ODL_ANNO"

         --"vsec"."DIL_VERNUM"

         --"vsec"."RATA_NUM"

    FROM  
    
asm_estratto_conto vsec,v_scritturadest de,SOGGETTO SO ,FATTURAT FAT,
(SELECT tfat_id,sum(pfat_tot) pfat_tot FROM FATTURAP WHERE pfat_flpiegen = 1 and az_id = '01' group by tfat_id) FAP

   WHERE 

            --AND     vsec.SOG_ID = 10703 -- in (10703,31260,9812,31030,9925)
            --and "vsec".par_id in (44045,174065)
            
          vsec.AZ_ID =de.az_id 

 
          
          --and de.mov_num = 81320
           and  de.par_id = vsec.par_id
           and  de.dst_id = vsec.dst_id
           and de.mov_num = vsec.mov_num
           AND SO.CSI_ID = vsec.CSI_ID
           AND FAT.PAR_ID(+) = vsec.par_id
           AND  FAP.TFAT_ID(+) =FAT.TFAT_ID
          
           --and vsec.par_id = 131961
           
           
ORDER BY        vsec.SOG_ID,
                vsec.PAR_ID,
                vsec.MOV_DT,
                vsec.MOV_NUM
   
       ;
                                               
                
        
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
          

( select ec.*,ra.rata_dtsca,ra.rata_imp,ra.rata_num from vsec ec,v_rata ra
where ec.az_id = '01'
and   ra.az_id(+) = ec.az_id
and   ec.tmov_id= 1 
and   ra.par_id(+)=ec.par_id 
and   ra.dil_staidsys(+) = 2
--and   ec.par_id = 389355
union
select ec.*,'','','' from vsec ec
where ec.az_id = '01'
and   ec.tmov_id<> 1 ) vsec


;                                             
                                               
select * from v_scritturadest where par_id = 131961;
select * from FATTURAT WHERE PAR_ID =290856 AND AZ_ID = 01;
SELECT * FROM FATTURAP WHERE TFAT_ID = 1294490;


        
